﻿using BasicInformation.ApiWrite.Application.Command.Client;
using BasicInformation.ErrorCode;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace BasicInformation.ApiWrite.Controllers
{
    /// <summary>
    /// 客户管理控制器——写
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class ClientController : ControllerBase
    {
        private readonly IMediator mediator;
        private readonly ILogger<ClientController> logger;

        public ClientController(IMediator mediator,ILogger<ClientController> logger)
        {
            this.mediator = mediator;
            this.logger = logger;
        }
        /// <summary>
        /// 客户管理添加接口
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public  Task<ApiResult<int>> AddClient(AddClientCommand request)
        {
            return mediator.Send(request);
        }
        /// <summary>
        /// 客户管理删除接口
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<int>> DelClient([FromQuery]DelClientCommand request)
        {
            return await mediator.Send(request);
        }
        /// <summary>
        /// 客户管理修改接口
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdateClient(UpdateClientCommand request)
        {
            return await mediator.Send(request);
        }
        /// <summary>
        /// 全局异常过滤器
        /// </summary>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        [HttpGet]
        public string Text()
        {
            try
            {
                logger.LogInformation("测试日志");
                throw new Exception("故意报错");
            }
            catch (Exception ex)
            {

                logger.LogError("测试日志==>" + ex.Message);
                throw new Exception("故意报错");
            }
        }
    }
}
